using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Weazel.Toad.Application.Forms
{
  public partial class NewProjectForm : Weazel.Toad.Application.Forms.DialogBaseForm
  {    
    public NewProjectForm()
    {
      InitializeComponent();

      // Just for the show ..
      projectTemplateListView.SelectedIndices.Add(0);
    }

    private void browseButton_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog dialog = new FolderBrowserDialog();

      if (dialog.ShowDialog() == DialogResult.OK)
      {        
        locationTextBox.Text = dialog.SelectedPath;
      }      
    }

    private string getProjectDirectoryString()
    {
      return locationTextBox.Text + "//" + nameTextBox.Text;
    }

    private string getFilePath()
    {
      return getProjectDirectoryString() + "//" + nameTextBox.Text + ".xprj";
    }

    protected override void OnClosing(CancelEventArgs e)
    {      
      if (DialogResult == DialogResult.OK)
      {        
        // check the location         
        if (!Directory.Exists(locationTextBox.Text))
        {
          System.Windows.Forms.MessageBox.Show("Location folder does not exist", "Warning");
          e.Cancel = true;
          return;
        }

        // check if the file already exists

        if(File.Exists(getFilePath()))
        {
          DialogResult overwrite = 
            MessageBox.Show("File already exists, continue?", "Warning", MessageBoxButtons.YesNo);

          if (overwrite == DialogResult.No)
          {          
            e.Cancel = true;
            return;
          }
        }

        // all is well, lets create the project

        // create the project directory, if needed
        if(!Directory.Exists(getProjectDirectoryString()))
          Directory.CreateDirectory(getProjectDirectoryString());

        // create the project
        Items.Project.CreateProject(getFilePath(), nameTextBox.Text);
      }
    }
  }
}

